<template>
  <view class="content">
    <view style="background: #fff">
      <basics-form
        ref="basicsForm"
        @submit="submit"
        @selectCallback="selectCallback"
        :slotData="{ codeWaitTime }"
        :hiddenMustIcon="hiddenMustIcon"
      >
        <template v-slot:inside="{ ruleKey, slotData }">
          <text
            style="margin-left: 40rpx"
            v-if="ruleKey == 'smsCode'"
            @click="getCode"
            :class="['my-active', slotData.codeWaitTime != 60 ? 'my-disabled' : '', 'login-code']"
          >
            {{ slotData.codeWaitTime == 60 ? "获取验证码" : slotData.codeWaitTime + "s后重试" }}
          </text>
        </template>
      </basics-form>
    </view>
    <view style="height: 200rpx" />
    <foot-button
      style="position: fixed; bottom: 0rpx; z-index: 10"
      :buttonList="buttonList"
      @tapButton="$refs.basicsForm.formValid()"
    />
  </view>
</template>

<script>
import basicsForm from "@/components/basics-form/basics-form.vue";
import footButton from "@/components/foot-button/foot-button.vue";

export default {
  components: {
    basicsForm,
    footButton,
  },
  data() {
    return {
      diseaseList: [],
      buttonList: [
        {
          name: `保存`,
          icon: "save",
        },
      ],

      hiddenMustIcon: false,
      addFlag: true,
      codeWaitTime: 60,
      timer: null,
      token: uni.getStorageSync("token"),
      doctorId: uni.getStorageSync("doctorId"),
      telphone: "",
    };
  },
  onShareAppMessage() {
    return this.$util.returnShareObj();
  },
  onLoad(options) {
    this.doctorDetail();
  },
  methods: {
    //获取患者信息
    doctorDetail() {
      this.$api.doctorDetail({ doctorId: this.doctorId }).then((res) => {
        if (res.code == 200) {
          this.doctorDictDataForDoctor(res.data);
        }
      });
    },
    //科室与职务
    doctorDictDataForDoctor(data) {
      this.$api.doctorDictDataForDoctor().then((res) => {
        if (res.code == 200) {
          let departmentIdIndex = "",
            postIdIndex = "";
          res.data.departmentList.some((item, index) => {
            if (item.deptId == data.departmentId) {
              departmentIdIndex = index;
              return true;
            }
          });
          res.data.postList.some((item, index) => {
            if (item.postId == data.postId) {
              postIdIndex = index;
              return true;
            }
          });
          console.log(departmentIdIndex, "===");
          this.initRules(data, res.data.departmentList, res.data.postList, departmentIdIndex, postIdIndex);
        }
      });
    },
    //医生数据，科室，职务
    initRules(data, departmentList, postList, departmentIdIndex, postIdIndex) {
      let rules = [
        {
          title: "姓名",
          key: "name",
          value: data.name || "",
          ruleList: ["minLength:2"],
          ruleMsgList: ["患者姓名至少2位"],
        },
        {
          title: "手机号",
          key: "phone",
          value: data.phone,
          tips: "不可编辑，需更换请联系医院管理员",
          disabled: true,
          skip: true,
        },
        {
          title: "工号",
          key: "jobNo",
          value: data.jobNo,
          tips: "不可编辑，需更换请联系医院管理员",
          disabled: true,
          skip: true,
        },
        {
          title: "性别",
          key: "sex",
          value: data.sex > -1 ? 1 - data.sex : "",
          type: "radio",
          selectList: [
            {
              name: "男",
              value: 1,
            },
            {
              name: "女",
              value: 0,
            },
          ],
          selectKey: "value",
          skip: true,
        },
        {
          title: "身份证号",
          key: "idCard",
          value: data.idCard || "",
          ruleList: ["isIdCard"],
          ruleMsgList: ["请输入正确的身份证号"],
          skip: true,
        },
        {
          title: "身份证照片",
          key: "idCardUrl",
          type: "img",
          value: data.idCardUrl || "",
          skip: true,
        },
        {
          title: "执业资格证",
          key: "certificateUrl",
          type: "img",
          value: data.certificateUrl || "",
          skip: true,
        },
        {
          title: "职业起始年份",
          key: "occupationStartYear",
          value: data.occupationStartYear || "",
          type: "year",
          endTime: this.$util.getTimeStr(new Date().getTime()),
          skip: true,
        },
        {
          title: "科室",
          key: "departmentId",
          value: departmentIdIndex,
          type: "select",
          selectList: departmentList,
          selectKey: "deptId",
          selectShowKey: "deptName",
          skip: true,
        },
        {
          title: "职务",
          key: "postId",
          value: postIdIndex,
          type: "select",
          selectList: postList,
          selectKey: "postId",
          selectShowKey: "postName",
          skip: true,
        },

        {
          title: "擅长病种",
          key: "goodAtDisease",
          value: data.goodAtDisease || "",
          type: "textarea",
          skip: true,
        },
        {
          title: "专业特长",
          key: "specialty",
          value: data.specialty || "",
          type: "textarea",
          skip: true,
        },
        {
          title: "简介",
          key: "introduction",
          value: data.introduction || "",
          type: "textarea",
          skip: true,
        },
      ];
      this.$nextTick(() => {
        console.log(this.$refs.basicsForm);
        this.$refs.basicsForm.initForm(rules);
      });
    },

    //提交
    submit(param) {
      if (!param) {
        return;
      }
      let imgs = [];
      if (param.idCardUrl && (param.idCardUrl.indexOf("blob") > 0 || param.idCardUrl.indexOf("tmp") > 0)) {
        imgs.push({
          key: "idCardUrl",
          name: "身份证照片",
          url: param.idCardUrl,
        });
      }
      if (
        param.certificateUrl &&
        (param.certificateUrl.indexOf("blob") > 0 || param.certificateUrl.indexOf("tmp") > 0)
      ) {
        imgs.push({
          key: "certificateUrl",
          name: "执业资格证",
          url: param.certificateUrl,
        });
      }
      if (imgs.length) {
        this.uploadPicture(imgs, 0);
      } else {
        delete param.avatar;
        param.doctorId = this.doctorId;
        this.$api.doctorUpdateDoctor(param).then((res) => {
          if (res.code == 200) {
            this.$util.showSuccessToast("档案已更新");
            setTimeout(() => {
              this.$util.backToRefreshPrevPage("index/index", "refreshDoctorData");
            }, 1500);
          }
        });
      }
    },
    //上传图片
    uploadPicture(imgs, index) {
      let data = imgs[index];
      this.$util.uploadPicture(data.url, data.name, (res) => {
        this.$refs.basicsForm.changeParamValue({
          key: data.key,
          value: res ? res.data : "",
        });
        if (index < imgs.length - 1) {
          this.uploadPicture(imgs, index + 1);
        } else {
          this.$refs.basicsForm.formValid();
        }
      });
    },
  },
};
</script>
<style lang="scss" scoped>
.content {
  background: $back-grey-color;
  height: 100vh;
}
</style>
